package com.tf_cut.backend.person.service;


import com.tf_cut.backend.common.pojo.PageInfo;
import com.tf_cut.backend.common.pojo.Result;
import com.tf_cut.backend.common.service.CommonServiceImpl;
import com.tf_cut.backend.mapper.PersonInfoMapper;
import com.tf_cut.backend.person.pojo.Person;
import com.tf_cut.backend.person.repository.PersonRepository;
import com.tf_cut.backend.person.vo.PersonAndAccountVo;
import com.tf_cut.backend.seller.vo.SellerBillAndConsigneeVo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Service
@Transactional
@Slf4j
public class PersonServiceImpl extends CommonServiceImpl<PersonAndAccountVo, Person, String> implements PersonService {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private PersonRepository personRepository;
    @Autowired
    private PersonAccountService personAccountService;
	@Autowired
	private PersonInfoMapper personInfoMapper;
    
	@Override
	public Result<PersonAndAccountVo> insertPersonAndAccount(PersonAndAccountVo svo) {
		Result<PersonAndAccountVo> result = this.save(svo);	// 新建个人
		svo.setPersonId(result.getData().getPersonId());
		result = personAccountService.save(svo);
		//svo.setSellerAccountId(result.getData().getSellerAccountId());	// 新建商户虚拟账号
		result.setData(svo);
		
		return result;
	}

	@Override
	public PageInfo<PersonAndAccountVo> queryPersonInfoList(PersonAndAccountVo personAndAccountVo) {
		com.github.pagehelper.PageHelper.startPage(personAndAccountVo.getPage(), personAndAccountVo.getRows());
		List<PersonAndAccountVo> list = personInfoMapper.queryPersonInfoList(personAndAccountVo);
		com.github.pagehelper.PageInfo<PersonAndAccountVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);

		PageInfo<PersonAndAccountVo> returnPageInfo = new PageInfo<>();
		returnPageInfo.setPage(pageInfo.getPageNum());
		returnPageInfo.setPageSize(pageInfo.getPageSize());
		returnPageInfo.setRows(pageInfo.getList());
		returnPageInfo.setRecords((int) pageInfo.getTotal());
		returnPageInfo.setTotal(pageInfo.getPages());
		return returnPageInfo;
	}

	@Override
	public PageInfo<PersonAndAccountVo> queryPersonInfo(int page, int rows, String personName, String startDate,
			String endDate,String cardStatus,String cardCode,String iphone) {
		if(!startDate.equals("")){
			startDate=startDate+" 00:00:00";
		}
		if(!endDate.equals("")){
			endDate=endDate+" 23:59:59";
		}
		com.github.pagehelper.PageHelper.startPage(page,rows);
        List<PersonAndAccountVo> list = personInfoMapper.queryPersonInfo(personName,startDate,endDate,cardStatus,cardCode,iphone);
        com.github.pagehelper.PageInfo<PersonAndAccountVo> pageInfo = new com.github.pagehelper.PageInfo<>(list);
		  PageInfo<PersonAndAccountVo> returnPageInfo = new PageInfo<>();
	        returnPageInfo.setPage(pageInfo.getPageNum());
	        returnPageInfo.setPageSize(pageInfo.getPageSize());
	        returnPageInfo.setRows(pageInfo.getList());
	        returnPageInfo.setRecords((int) pageInfo.getTotal());
	        returnPageInfo.setTotal(pageInfo.getPages());
	        return returnPageInfo;
	}

	@Override
	public PersonAndAccountVo findbyqueryPerson(String identityCardNum) {
		return personInfoMapper.findbyqueryPerson(identityCardNum);
	}

	@Override
	public PersonAndAccountVo queryByPersonId(String personId) {
		return personInfoMapper.queryByPersonId(personId);
	}

}
